package com.meihua.baoleyi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.bean.ConsumerRebateBean;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.utils.MD5;
import com.meihua.baoleyi.utils.UrlContants;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 消费返利
 * Created by liming on 2017/10/18.
 */

public class ConsumerRebateActivity extends BaseActivity {

    @Bind(R.id.rl_name)
    RelativeLayout rlName;
    private TextView mTv_guize;//返利规则
    private TextView mTv_diffmoney;//返利金额还差
    private TextView mTv_totalmoney;//本月累计消费金额
    private TextView btnSubmit;
    private TextView tx1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consumer_rebate);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        mTv_guize = findViewById(R.id.tv_guize);
        mTv_diffmoney = findViewById(R.id.tv_diffmoney);//返利金额还差
        mTv_totalmoney = findViewById(R.id.tv_totalmoney);//本月累计消费金额
        btnSubmit = findViewById(R.id.submit);
        tx1 = findViewById(R.id.tx1);
        btnSubmit.setOnClickListener(this);
        mTv_guize.setOnClickListener(this);
        if (myApp.getLevel() == 3) {
            btnSubmit.setVisibility(View.VISIBLE);
        }
        PriceData();
    }

    /**
     * 解析服务器数据
     */
    private void PriceData() {
        showDialog();
        String userid = myApp.getUser_id();
        long time = System.currentTimeMillis() / 1000;//获取系统时间的10位的时间戳
        String str = String.valueOf(time);
        String token = myApp.getToken().toString();
        String sign_none = "id=" + userid + "&timestamp=" + str + "&token=" + token;
        String sign_yes = MD5.getMessageDigest(sign_none.toString().getBytes()).toUpperCase();
        OkGo.get(UrlContants.Consumer_Rebate + "?id=" + userid + "&timestamp=" + str + "&token=" + token + "&sign=" + sign_yes)
                .tag(this)
                .params("id", userid)
                .params("timestamp", str)//时间戳
                .params("token", token)//token
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (s != null) {
                            try {
                                JSONObject jsonObject = new JSONObject(s);
                                if (jsonObject.optString("code").equals("200")) {
                                    String date = jsonObject.optString("data");
                                    ConsumerRebateBean detailList = JSON.parseObject(date, ConsumerRebateBean.class);
                                    if (!detailList.getDiff().equals("0")) {
                                        mTv_diffmoney.setText("距离返利金额还差" + detailList.getDiff() + "元");
                                    } else {
                                        rlName.setVisibility(View.GONE);
                                    }
                                    mTv_totalmoney.setText(detailList.getMoney());
                                    Toast.makeText(ConsumerRebateActivity.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                                } else {
                                    tx1.setText(jsonObject.optString("msg"));
//                                    Toast.makeText(ConsumerRebateActivity.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        dismissDialog();
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        dismissDialog();
                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_guize://返利规则
                Intent intent = new Intent(this, CommonWebActivity.class);
                intent.putExtra("url", APIServer.FLRULES);
                startActivity(intent);
                break;
            case R.id.submit://申请返利
                apply();
                break;
        }
    }

    /*申请返利*/
    private void apply() {
        showDialog();
        TreeMap<String, String> treeMap = new TreeMap<String, String>();
        treeMap.put("id", MyApp.getInstance().getUser_id());//地址记录id
        NetHelper.get(APIServer.REBATE_APPLY, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                JSONObject object = null;
                try {
                    object = new JSONObject(s);
                    if (object.getString("code").equals("200")) {
                        Toast.makeText(ConsumerRebateActivity.this, "申请成功", Toast.LENGTH_SHORT).show();
                        btnSubmit.setVisibility(View.GONE);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                dismissDialog();
            }
        });
    }

    @OnClick(R.id.text_back) //返回
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
